package com.aaa.service.impl;

import com.aaa.mapper.BillMapper;
import com.aaa.mapper.CheckMapper;
import com.aaa.mapper.OrderMapper;
import com.aaa.mapper.OrderdetailsMapper;
import com.aaa.service.OrderdetailsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class OrderdetailsServiceImpl implements OrderdetailsService {
    @Resource
    private OrderdetailsMapper orderdetailsMapper;
    @Resource
    private BillMapper billMapper;
    @Resource
    private CheckMapper checkMapper;
    @Resource
    private OrderMapper orderMapper;
    @Override
    public Object finddetails(Integer oid) throws ParseException {
        List<Map<String,Object>> list=orderdetailsMapper.finddetails(oid);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now=new Date();
        if(list.size()>0&&list!=null){
            System.out.println(list.size());
            for (int i = 0; i < list.size(); i++) {
                System.out.println(list.get(i));
                Date enddate=null;
                if(list.get(i).get("ostate").equals("已入住")){
                    System.out.println("已入住");
                    enddate=  sdf.parse(list.get(i).get("enddate").toString());
                    if(now.getTime()>=enddate.getTime()){
                        System.out.println("已到期");
                        Integer id= (Integer) list.get(i).get("odid");
                        System.out.println("退房时间："+id);
                        //修改已到达退房时间的订单状态
                        orderdetailsMapper.OverTimeState(id,"待退房");
                        list=orderdetailsMapper.finddetails(oid);
                    }
                }
            }
            return list;
        }
        return list;
    }

    @Override
    public List<Map<String, Object>> findbyOdid(Integer oid) throws ParseException {
        List<Map<String, Object>> list=orderdetailsMapper.findbyOdid(oid);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now=new Date();
        if(list.size()>0&&list!=null){
            System.out.println(list.size());
            for (int i = 0; i < list.size(); i++) {
                System.out.println(list.get(i));
                Date enddate=null;
                if(list.get(i).get("state").equals("已入住")){
                    System.out.println("已入住");
                    enddate=  sdf.parse(list.get(i).get("enddate").toString());
                    if(now.getTime()>=enddate.getTime()){
                        System.out.println("已到期");
                        Integer id= (Integer) list.get(i).get("id");
                        System.out.println("退房时间："+id);
                        //修改已到达退房时间的订单状态
                        orderdetailsMapper.OverTimeState(id,"待退房");
                        list=orderdetailsMapper.findbyOdid(oid);
                    }
                }
            }
            return list;
        }
        return list;
    }

    @Override
    public List<Map<String, Object>> findroombyid(Integer id) {
        return orderdetailsMapper.findroombyid(id);
    }

    @Override
    public Integer xufang(Integer odid, String checkout, double money, String remark, String day, Integer staff) {
        String operate="续房"+day+"天";
        Integer oid= (Integer) orderdetailsMapper.findorder(odid).get("orderid");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        orderMapper.changemoney(money,oid);
        //结束时间
        checkout= checkout.replace("T"," ").substring(0,19);
        billMapper.addbill(operate,money,odid,remark,staff);
        return checkMapper.xufang(odid,checkout);
    }

}
